package com.usc.csci588.chaltrojan.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.usc.csci588.chaltrojan.R;

public class TwitterLoginActivity extends Activity {
	
	
	//private CommonsHttpOAuthConsumer httpOauthConsumer;
	//private OAuthProvider httpOauthprovider;

	Button startButton;
	EditText handle_et;	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Toast.makeText(getBaseContext(), "Let's authenticate using twitter!!",Toast.LENGTH_LONG).show();		
		//startOAuth();
		setContentView(R.layout.gettwitterhandle);
		askForTwitterHandle();
		//appstate = ((MyApp)this.getApplicationContext());

	}
	
	public void askForTwitterHandle()
	{
	
		startButton = (Button)findViewById(R.id.startButton);
		handle_et = (EditText)findViewById(R.id.tweeterHandle);
		
		startButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String handle = handle_et.getText().toString();				
				if (handle == "" || handle.equals(null))
				{
					handle_et.setError("Please enter a handle.");					
				}
				else
				{		
					Toast.makeText(getApplicationContext(), "Welcome! "+ handle,Toast.LENGTH_LONG).show();
					SharedPreferences settings = getSharedPreferences("HANDLE", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("handle",handle);
                    //editor.putString("user_name", username.getText().toString());
                    editor.commit();					
			//		appstate.setHandle(handle);
					Intent i = new Intent(getApplicationContext(),com.usc.csci588.chaltrojan.login.MainMenuActivity.class);	            
			        startActivity(i); 					
					
				}			
				
			}
		});
		
	}
	
/*
	public void startOAuth() {
		try {
			httpOauthConsumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY,Constants.CONSUMER_SECRET);
			Log.d("twitter", "httpOauthConsumer done ");
			httpOauthprovider = new DefaultOAuthProvider(
					Constants.REQUEST_URL,
					Constants.ACCESS_URL,
					Constants.AUTHORIZE_URL);
			Log.d("twitter", "httpOauthprovider done ");
			String authUrl = httpOauthprovider.retrieveRequestToken(
					httpOauthConsumer, Constants.OAUTH_CALLBACK_URL);
			// Open the browser
			Log.d("twitter", "Abt to start new activity ");
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));			
		} catch (Exception e) {
			Log.d("twitter", "Oath ERR --> "+e.getMessage());
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	
*/	
/*	
	@Override
	protected void onNewIntent(Intent intent) {
	    super.onNewIntent(intent);

	    Uri uri = intent.getData();
	    //Check if you got NewIntent event due to Twitter Call back only
	    if (uri != null && uri.toString().startsWith(Constants.OAUTH_CALLBACK_URL)) {

	        String verifier = uri.getQueryParameter  (oauth.signpost.OAuth.OAUTH_VERIFIER);

	        try {
	            // this will populate token and token_secret in consumer

	            httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
	            String userKey = httpOauthConsumer.getToken();
	            String userSecret = httpOauthConsumer.getTokenSecret();

	            // Save user_key and user_secret in user preferences and return
	            SharedPreferences settings = getBaseContext().getSharedPreferences("your_app_prefs", 0);
	            SharedPreferences.Editor editor = settings.edit();
	            editor.putString("user_key", userKey);
	            editor.putString("user_secret", userSecret);
	            editor.commit();          
	                        
	            
	            HttpParameters params1 = httpOauthprovider.getResponseParameters();
	            String userName = params1.getFirst("screen_name");
	            
	            Toast.makeText(getBaseContext(), "Successfully Authenticated user: "+ userName,
	    				Toast.LENGTH_LONG).show();
	            
	            //Log.v("user name >>>>>>>>", userName);
	            Intent i = new Intent(getApplicationContext(),MainMenuActivity.class);	            
	            startActivity(i);        
	            
      
	        } catch(Exception e){
	        	e.printStackTrace();
	        }
	    } else {
	        // Do something if the callback comes from elsewhere
	    }

	}
*/

}
